﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class ConfigParmController : Base
{
    private readonly ConfigParmService _configParmService;
    public ConfigParmController(ConfigParmService configParmService)
    {
        _configParmService = configParmService;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ConfigParm_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery] QueryConfigParm query)
    {
        PagedInfo<ConfigParm> pagedList = await _configParmService.GetPageListAsync(query);
        return Success(new { count = pagedList.TotalNum, data = pagedList.Result });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ConfigParm_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        ConfigParm data = await _configParmService.GetIdAsync(id);
        return Success(new { data });
    }
    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.ConfigParm_AddOrEdit)]
    [Log(Name = "新增参数设置")]
    public async Task<IActionResult> CreatePostAsync([FromBody] ConfigParm configParm)
    {
        if (await _configParmService.InsertDelCache(configParm) != null)
            return Success("新增参数设置: " + configParm.Name);
        else
            return Fail("失败，可能该编码已经存在");
    }
    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.ConfigParm_AddOrEdit)]
    [Log(Name = "修改参数设置")]
    public async Task<IActionResult> EditPostAsync([FromBody] ConfigParm configParm)
    {
        if (await _configParmService.UpdateDelCache(configParm) > 0)
            return Success("成功参数设置：" + configParm.Name);
        else
            return Fail("失败，可能编码已经存在");
    }
    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.ConfigParm_Delete)]
    [Log(Name = "删除参数设置")]
    public async Task<IActionResult> Delete(int id)
    {
        if (await _configParmService.DeleteAndDelCacheAsync(id) > 0)
            return Success("删除参数设置 ID：" + id);
        else
            return Fail("删除失败！");
    }
}
